<?php

class model_tags extends CI_Model {
    
    function __construct() {
        parent::__construct();
    }

    public function get_tag($tagID) {
        $this->db->where('id_tags', $tagID);
        $query = $this->db->get('tags');
        return $query->row_array();
    }
    
    public function add_tag($content, $book, $chap,$location) {
        $id = 'tag'.time();
        $data = array(
            'id_tags' => $id,
            'name_tags' => $content,
            'id_doc' => $book,
            'id_chap' => $chap,
            'location' => $location
        );
        $this->db->insert('tags', $data);
        return $id;
    }

    public function remove_tag($tagid) {
        $this->db->where('id_tags', $tagid);
        $this->db->delete('tags');
    }

    public function get_user_tag($user) {
        $this->db->where('username', $user);
        $sql = "SELECT * FROM document AS doc, categories as cat, tags 
        WHERE doc.id_document = tags.id_doc AND doc.category = cat.id_cat
             AND doc.username = '$user'";
        $query = $this->db->query($sql);

        return $query->result_array();
    }   
    
    public function searchTag($text) {
        $query = $this->db->query ( "SELECT * FROM document AS doc, categories AS cat, tags 
            WHERE doc.category = cat.id_cat AND tags.id_doc = doc.id_document
            AND name_tags LIKE '%$text%'");
        
        $list = $query->result_array();
        return $list;
    }

    public function searchLocation($text) {
        $query = $this->db->query ( "SELECT * FROM document AS doc, categories AS cat, tags, location WHERE doc.category = cat.id_cat AND tags.id_doc = doc.id_document
            AND tags.location = location.id_location AND name_location LIKE '%$text%'");
        
        $list = $query->result_array();
        return $list;
    }

    public function getLocation($parent) {
        $this->db->where('parent_location', $parent);
        $query = $this->db->get('location');
        $list = $query->result_array();
        return $list;
    }

/**
 * ================================================================
*/
}
?>